/**
 * @author wn
 * @date 2023/12/14 11:15:03
 * @description: 订单 API 接口
 */

import { request } from '@/utils/request'

enum Url {
	Order = '/member/order/pre',
	OrderNow = '/member/order/pre/now',
	OrderMember = '/member/order',
	OrderAgain = '/member/order/repurchase/',
	OrderDelivery = '/member/order/consignment/',
}

/**
 * 获取 创建订单页信息 -- 由购物车进入,有选中的商品,进入后 直接后台获取
 * @returns Promise
 */
export const getCreateOrderDataApi = () =>
	request<CreateOrderData>({
		method: 'GET',
		url: Url.Order,
	})

/**
 * 获取 创建订单页信息 -- 商品详情页直接购买
 * @param data -- skuId 数量 地址di
 * @returns Promise
 */
export const getCreateOrderDataNowApi = (data: {
	skuId: string
	count: string
	addressId?: string
}) =>
	request<CreateOrderData>({
		method: 'GET',
		url: Url.OrderNow,
		data,
	})

/**
 * 获取 创建订单页信息 -- 再次购买
 * @param data -- id -- 订单编号
 * @returns Promise
 */
export const getCreateOrderDataAgainApi = (id: string) =>
	request<CreateOrderData>({
		method: 'GET',
		url: `${Url.OrderAgain}${id}`,
	})

/**
 * 提交订单
 * @param data -- 订单页收集数据
 * @returns Promise -- 订单编号
 */
export const postOrderApi = (data: OrderCreateParams) =>
	request<OrderCreateResult>({
		method: 'POST',
		url: Url.OrderMember,
		data,
	})

/**
 * 获取订单详情
 * @param id -- 当单编号
 * @returns Promise
 */
export const getOrderDetailApi = (id: string) =>
	request<OrderDetail>({
		method: 'GET',
		url: `${Url.OrderMember}/${id}`,
	})

/**
 * 模拟发货 -- 实际应由商家发货
 * @param id -- 订单 id
 * @returns Promise<Any>
 */
export const getOrderConsignmentApi = (id: string) =>
	request({
		method: 'GET',
		url: `${Url.OrderDelivery}${id}`,
	})

/**
 * 确认收货
 * @param id -- 订单 id
 * @returns Promise
 */
export const putOrderConfirmApi = (id: string) =>
	request<OrderDetail>({
		method: 'PUT',
		url: `/member/order/${id}/receipt`,
	})

/**
 * 删除订单 -- 仅在订单状态为 待评价，已完成，已取消时，可删除订单。
 * @param data -- ids 订单集合
 * @returns Promise<Any>
 */
export const deleteOrderApi = (data: { ids: string[] }) =>
	request({
		method: 'DELETE',
		url: Url.OrderMember,
		data,
	})

/**
 * 取消订单 -- 仅在订单状态为待付款时，可取消订单。
 * @param id 订单id
 * @param data -- cancelReason 取消理由
 * @returns Promise
 */
export const putOrderCancelApi = (id: string, data: { cancelReason: string }) =>
	request<OrderDetail>({
		method: 'PUT',
		url: `/member/order/${id}/cancel`,
		data,
	})

/**
 * 获取订单物流 -- 仅在订单状态为待收货，待评价，已完成时，可获取物流信息。
 * @param id -- 订单id
 * @returns Promise<Any>
 */
export const getOrderLogisticsApi = (id: string) =>
	request<OrderLogistic>({
		method: 'GET',
		url: `/member/order/${id}/logistics`,
	})

/**
 * 获取订单列表 -- change 触发
 * @param data -- 分页 + 订单状态
 * @returns Promise
 */
export const getOrderListApi = (data: OrderListParams) =>
	request<OrderListResult>({
		method: 'GET',
		url: Url.OrderMember,
		data,
	})
